package org.ben.birthday.twentybirthday.service.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2019/9/10 10:49
 */
@Controller
@RequestMapping("/oss")
public class OssTest<buckets> {
    @RequestMapping("/test")
    @ResponseBody
    public  ObjectListing test(){
        String secretId = "AKID3tvlocqVZNZVChA3SccMr7C2hMmyvPz1";
        String secretKey = "1nMEUZOyJVhiV2KRKFvMitvRlqrpKn49";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
//        Region region = new Region("ap-guangzhou");
        Region region=new Region("ap-beijing");
        ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
//    COSCredentials cosCredentials=new BasicCOSCredentials("secretId","secretKey");
//    Region region=new Region("ap-beijing");
//    ClientConfig clientConfig=new ClientConfig(region);
//    COSClient cosClient=new COSClient(cosCredentials,clientConfig);
        System.out.println("-------------------------");
//    try {
//            List<Bucket> buckets = cosClient.listBuckets();
//            System.out.println(buckets);
//            return buckets;
//        } catch (CosServiceException serverException) {
//            serverException.printStackTrace();
//            return null;
//        } catch (CosClientException clientException) {
//            clientException.printStackTrace();
//            return null;
//    }

        try {
            String bucket = "ben-1259308595";
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
            // 设置 bucket 名称
            listObjectsRequest.setBucketName(bucket);
            // prefix 表示列出的 object 的 key 以 prefix 开始
            listObjectsRequest.setPrefix("");
            // 设置最大遍历出多少个对象, 一次 listobject 最大支持1000
            listObjectsRequest.setMaxKeys(1000);
            listObjectsRequest.setDelimiter("/");
            ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
            for (COSObjectSummary cosObjectSummary : objectListing.getObjectSummaries()) {
                // 对象的路径 key
                String key = cosObjectSummary.getKey();
                // 对象的 etag
                String etag = cosObjectSummary.getETag();
                // 对象的长度
                long fileSize = cosObjectSummary.getSize();
                // 对象的存储类型
                String storageClass = cosObjectSummary.getStorageClass();
                System.out.println("key:" + key + "; etag:" + etag + "; fileSize:" + fileSize + "; storageClass:" + storageClass);
            }
           return objectListing;
        } catch (CosServiceException serverException) {
            serverException.printStackTrace();
            return null;
        } catch (CosClientException clientException) {
            clientException.printStackTrace();
            return null;
        }
    }
}